"""
@Time ： 2024/8/23 12:30
@Auth ： jany
"""
import logging

from pymysql import Connection
import random
import string
import requests
#随机生成字符串
def generate_random_string(length):
    letters = string.ascii_letters + string.digits
    return ''.join(random.choice(letters) for _ in range(length))

random_string = generate_random_string(15)



#skucode转化为金蝶code
def skucode2jindecode(skucode):
    try:
        con = Connection(
            host="hosturl",
            port=3306,
            user="user",
            password="password",
            database="database"
        )
        #con.database ="database"
        # 创建游标对象
        cursor = con.cursor()
        # 定义一个表sql
        sql = 'select jin_die_code from product_sku ps  where sku_code in ("'+skucode+'")'
        cursor.execute(sql)
        result = cursor.fetchall()

        print("skucode============"+skucode)
        return result[0][0]
    except Exception as e:
        print("异常", e)
    con.close()


#将券的状态设置为已审批的状态
def updatecouponstatus (couponno):
    try:
        con = Connection(
            host="host",
            port=3306,
            user="user",
            password="password",
            database="database"
        )

        # 创建游标对象
        cursor = con.cursor()
        # 定义一个表sql
        sql = 'update coupon_basis_info set approval_status = 3 where coupon_no ="'+couponno+'"'
        cursor.execute(sql)
        con.commit()
    except Exception as e:
        print("异常", e)
        con.rollback()
    con.close()

def mobiletouserid(useridOrMobile):
    userid=useridOrMobile;
    if len(useridOrMobile)==11 :
        url = "url"
        headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Accept": "application/json"
        }
        data = {
            "model": '{"phone":"' + useridOrMobile + '"}'
        }
        response = requests.post(url, headers=headers, data=data)
        userid = response.json()['data']['userId']
    return str(userid)


#重置用户的积分值 memberPointModify
def memberPointModify (userid,pointNum):
    try:
        con = Connection(
            host="url",
            port=3306,
            user="user",
            password="password",
            database="database"
        )
        #con.database ="guoquan_customer"
        # 创建游标对象
        cursor = con.cursor()
        # 定义一个表sql
        sql = 'update cus_integral_info set integral_value = '+pointNum+' where member_id ="'+userid+'"'
        logging.info(sql)
        cursor.execute(sql)
        con.commit()
    except Exception as e:
        print("异常", e)
        con.rollback()
    con.close()


#重置会员level memberLevelModify

def memberLevelModify (userid,userLevel):
    try:
        con = Connection(
            host="url",
            port=3306,
            user="url",
            password="password",
            database="database"
        )
        #con.database ="guoquan_customer"
        # 创建游标对象
        cursor = con.cursor()
        # 定义一个表sql
        sql = 'update member_level_info set level_value = '+userLevel+' where member_id ="'+userid+'"'
        logging.info(sql)
        cursor.execute(sql)
        con.commit()
    except Exception as e:
        print("异常", e)
        con.rollback()
    con.close()